﻿Option Explicit On

Imports System.IO

Public Class Logger
    '*******************************************************************************************
    '* CLASS_MODULE:    Logger
    '* AUTHOR:          Cliff Gibb - EverAge Consulting Inc. - cgibb@everage.ca
    '*
    '* PURPOSE:         The Logger object stores the information for this logger:
    '*                      - the log level (info,warn,fatal)
    '*                      - the destination log file
    '*                      - the "Class Name" (or Sub name in the case of a VBA logger)
    '*                  The Logger class also contains the subroutines to log info/warn/fatal messages.
    '*******************************************************************************************
    Const INFO_LEVEL_TEXT$ = "INFO: "
    Const WARN_LEVEL_TEXT$ = "WARN: "
    Const ERROR_LEVEL_TEXT$ = "ERROR: "
    Const FATAL_LEVEL_TEXT$ = "FATAL:"

    Private sThisLogFilePath$
    Private sThisSubName$
    Private iThisLogLevel%

    Public Property LogFilePath() As String
        Set(ByVal value As String)
            sThisLogFilePath = value
        End Set
        Get
            Return sThisLogFilePath
        End Get
    End Property

    Public Property SubName() As String
        Set(ByVal value As String)
            sThisSubName = value
        End Set
        Get
            Return sThisSubName
        End Get
    End Property

    Public Property LogLevel() As Integer
        Set(ByVal value As Integer)
            iThisLogLevel = value
        End Set
        Get
            Return iThisLogLevel
        End Get
    End Property

    '*******************************************************************************************
    '* NAME:        info
    '*
    '* PURPOSE:     To log an 'info' message if the current log level is set to INFO.
    '*
    '* PARAMETERS:  sLogText - the text to log
    '*
    '*******************************************************************************************
    Public Sub info(ByVal sLogText As String)
        If Me.LogLevel = LoggerFactory.INFO_LEVEL Then
            Call WriteLog(LoggerFactory.INFO_LEVEL, sLogText)
        End If
    End Sub

    '*******************************************************************************************
    '* NAME:        warn
    '*
    '* PURPOSE:     To log a 'warn' message if the current log level is set to INFO or WARN.
    '*
    '* PARAMETERS:  sLogText - the text to log
    '*
    '*******************************************************************************************
    Public Sub warn(ByVal sLogText As String)
        If Me.LogLevel < LoggerFactory.ERROR_LEVEL Then
            Call WriteLog(LoggerFactory.WARN_LEVEL, sLogText)
        End If
    End Sub

    '*******************************************************************************************
    '* NAME:        err
    '*
    '* PURPOSE:     To log a 'err' message if the current log level is set to INFO or WARN or ERROR.
    '*
    '* PARAMETERS:  sLogText - the text to log
    '*
    '*******************************************************************************************
    Public Sub err(ByVal sLogText As String)
        If Me.LogLevel < LoggerFactory.FATAL_LEVEL Then
            Call WriteLog(LoggerFactory.ERROR_LEVEL, sLogText)
        End If
    End Sub

    '*******************************************************************************************
    '* NAME:        fatal
    '*
    '* PURPOSE:     To log a 'fatal' message if the current log level is set to INFO or WARN or FATAL.
    '*
    '* PARAMETERS:  sLogText - the text to log
    '*
    '*******************************************************************************************
    Public Sub fatal(ByVal sLogText As String)
        If Me.LogLevel <= LoggerFactory.FATAL_LEVEL Then
            Call WriteLog(LoggerFactory.FATAL_LEVEL, sLogText)
        End If
    End Sub

    '*******************************************************************************************
    '* NAME:        WriteLog
    '*
    '* PURPOSE:     To write the appropriate log message to disk.
    '*
    '* PARAMETERS:  iLogLevel - the log level for this entry
    '*              sLogText - the text to log
    '*
    '*******************************************************************************************
    Private Sub WriteLog(ByVal iLogLevel As Integer, ByVal sLogText As String)
        Dim LogMessage As String
        Dim sDateTime As String
        Dim sLogLevel As String

        If iLogLevel = LoggerFactory.INFO_LEVEL Then
            sLogLevel = INFO_LEVEL_TEXT
        ElseIf iLogLevel = LoggerFactory.WARN_LEVEL Then
            sLogLevel = WARN_LEVEL_TEXT
        ElseIf iLogLevel = LoggerFactory.ERROR_LEVEL Then
            sLogLevel = ERROR_LEVEL_TEXT
        ElseIf iLogLevel = LoggerFactory.FATAL_LEVEL Then
            sLogLevel = FATAL_LEVEL_TEXT
        Else
            sLogLevel = "!INVALID LOG LEVEL!"
        End If
        sDateTime = CStr(Now())
        LogMessage = sLogLevel & " [" & Me.SubName & "] (" & sDateTime & ") - " & sLogText
        Debug.Print(LogMessage) ' Send to the immediate window then to the text file

        Using sw As StreamWriter = New StreamWriter(Me.LogFilePath, True)
            sw.WriteLine(LogMessage)
        End Using

    End Sub

End Class